package com._21cn.cms.yellow.web.controller;

import com._21cn.cms.yellow.entity.YellowPageCate;
import com._21cn.cms.yellow.service.YellowPageCateService;
import com._21cn.framework.base.controller.BaseController;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.http.HttpRequestInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({"/admin/yellow/yellowPageCate"})
public class YellowPageCateController extends BaseController
{
    private static String listView = "/admin/yellow/yellowPageCate/list";
    private static String showView = "/admin/yellow/yellowPageCate/show";
    private static String jsonView = "/admin/yellow/yellowPageCate/json";
    private static String formView = "/admin/yellow/yellowPageCate/form";
    private static String redirectListView = "redirect:/admin/yellow/yellowPageCate/list.do";

    private Map<String, String> propertyTitleMap = null;

    @Autowired
    private YellowPageCateService yellowPageCateService;

    @Autowired(required=false)
    @Qualifier("yellowPageCateValidator")
    private Validator yellowPageCateValidator;

    @RequestMapping({"/list"})
    public String listYellowPageCate(HttpServletRequest request, HttpServletResponse response, ModelMap model) { HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", listView);
        setRequestModelMap(request, model, true);
        return view; }

    @RequestMapping({"/search"})
    public String searchYellowPageCate(HttpServletRequest request, HttpServletResponse response, ModelMap model)
    {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", jsonView);
        setRequestModelMap(request, model, true);
        PageList result = getPageList(model);
        model.addAttribute("objListJson", JSON.toJSONString(result, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat }));
        return view;
    }

    @RequestMapping({"/show"})
    public String showYellowPageCate(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", showView);
        Integer id = Integer.valueOf(reqInfo.getIntParameter("id", -1));
        if (id.intValue() > 0) {
            YellowPageCate yellowPageCate = (YellowPageCate)this.yellowPageCateService.getById(id);
            if (yellowPageCate == null) {
                model.put("error.param.wrong", "无效参数");
            }

            model.addAttribute("yellowPageCate", yellowPageCate);
        }
        setRequestModelMap(request, model);
        return view;
    }

    @RequestMapping({"/edit"})
    public String editYellowPageCate(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter("view", formView);
        Integer id = Integer.valueOf(reqInfo.getIntParameter("id", -1));
        YellowPageCate yellowPageCate = new YellowPageCate();
        if (id.intValue() > 0) {
            yellowPageCate = (YellowPageCate)this.yellowPageCateService.getById(id);
            if (yellowPageCate == null) {
                model.put("error.param.wrong", "无效参数");
            }
        }
        model.addAttribute("yellowPageCate", yellowPageCate);

        setRequestModelMap(request, model);
        return view;
    }

    @RequestMapping({"/save"})
    public String saveYellowPageCate(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = Integer.valueOf(reqInfo.getIntParameter("id", -1));
        if (id.intValue() > 0) {
            YellowPageCate yellowPageCate = (YellowPageCate)this.yellowPageCateService.getById(id);
            if (bind(request, yellowPageCate, model, new Validator[] { this.yellowPageCateValidator }).hasErrors()) {
                return formView;
            }
            this.yellowPageCateService.update(yellowPageCate);
        } else {
            YellowPageCate yellowPageCate = new YellowPageCate();
            if (bind(request, yellowPageCate, model, new Validator[] { this.yellowPageCateValidator }).hasErrors()) {
                return formView;
            }
            this.yellowPageCateService.insert(yellowPageCate);
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
    }

    @RequestMapping({"/updateStatus"})
    public String updateYellowPageCateStatus(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer status = Integer.valueOf(reqInfo.getIntParameter("status", -1));
        if (status.intValue() < 0) {
            model.clear();
            return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
        }
        setRequestModelMap(request, model);
        String objectIds = getGetObjectIds(reqInfo, model);
        if (objectIds.length() > 0) {
            this.yellowPageCateService.update(model);
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
    }

    @RequestMapping({"/delete"})
    public String deleteYellowPageCate(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        String objectIds = getGetObjectIds(reqInfo, model);
        if (objectIds.length() > 0) {
            this.yellowPageCateService.delete(model);
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter("queryParam", "");
    }

    private PageList<YellowPageCate> getPageList(ModelMap model) {
        if (model.containsKey("likeName"))
            model.put("likeName", model.get("likeName").toString().toLowerCase());
        PageList yellowPageCateList = this.yellowPageCateService.getList(model, Integer.parseInt(model.get("pageNo").toString()), Integer.parseInt(model.get("pageSize").toString()));
        model.addAttribute("yellowPageCateList", yellowPageCateList);
        model.addAttribute("objList", yellowPageCateList);
        model.addAttribute("pageTurn", yellowPageCateList.getPageTurn());
        model.addAttribute("titleMap", getPropertyTitleMap());
        return yellowPageCateList;
    }

    private Map<String, String> getPropertyTitleMap() {
        if (this.propertyTitleMap == null) {
            this.propertyTitleMap = new HashMap();
            this.propertyTitleMap.put("id", "id");
            this.propertyTitleMap.put("yellowCateId", "yellowCateId");
            this.propertyTitleMap.put("yellowPageId", "yellowPageId");
            this.propertyTitleMap.put("createTime", "createTime");
        }
        return this.propertyTitleMap;
    }
}